var DEBUG = true;

function debug(msg) {
	if(DEBUG) {
		$('#debug').append(msg+'<br />');
	}
}

$(document).ready(function() {
	$('#action_asked #templates').hide();
	$('#action_asked #response').hide();
	$('#action_asked .hint').hide();
	
	$('#action_asked #questions').empty();	// NOT NESCESSARY
	
	$('#action_asked #response').load(
		'askedQuestions', 
		function() {
			var strData = $(this).text();
			debug(strData);
			var objData = jQuery.parseJSON(strData);
			if(objData.questions != null && objData.questions.length > 0) {
				var q = objData.questions;
				for (var i in q) {
					debug(q[i].content);
					$('#action_asked #templates .question_item').clone().appendTo('#action_asked #questions');
					$('#action_asked #questions .question_item:last-child .content').text(q[i].content);
					$('#action_asked #questions .question_item:last-child .date').text(q[i].date);
					for (var j in q[i].answers) {
						debug(q[i].answers[j].content);
						$('#action_asked #templates .answer_item').clone().appendTo('#action_asked #questions .question_item:last-child .answers');
						$('#action_asked #questions .question_item:last-child .answers .answer_item:last-child .content').text(q[i].answers[j].content);
						$('#action_asked #questions .question_item:last-child .answers .answer_item:last-child .date').text(q[i].answers[j].date);
						$('#action_asked #questions .question_item:last-child .answers .answer_item:last-child .firstname').text(q[i].answers[j].firstname);
					}
				}
				
				$('#action_asked #questions .question_item .question').click(function() {
					if($(this).parent().children('.answers').attr('style') == 'display: none;') {
						$(this).parent().children('.answers').slideDown(60);
					} else {
						$(this).parent().children('.answers').slideUp(60);
					}
				});
			} else if(objData.redirect != null) {
				window.location = objData.redirect;
			} else {
				$('#action_asked #history_warning').text('You have not asked a question.');
				$('#action_asked #history_warning').show();
			}
		}
	);
});